package com.enze.springbootmail;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
/**
 * @Author TacoMan
 * @Date 2023/5/29 10:35
 * @Version 1.0
 */
public class demo {



            /**
             * 根据时间获取月的第几周
             * @param sourceTime
             * @return
             */
            public static String getMonthNoAndWeekNo(LocalDateTime sourceTime) {
                Date date = Date.from(sourceTime.atZone(ZoneId.systemDefault()).toInstant());
                return join(date);
            }

            /**
             * 根据时间获取月的第几周
             * @param sourceTime
             * @return
             */
            public static String getMonthNoAndWeekNo(LocalDate sourceTime) {
                Date date = Date.from(sourceTime.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
                return join(date);
            }

            /**
             * 根据时间获取月的第几周
             * @param date
             * @return
             */
            public static String getMonthNoAndWeekNo(Date date) {
                return join(date);
            }

            /**
             * 拼接
             * @param date
             * @return
             */
            public static String join(Date date){
                return getMonth(date) + "-" + getWeek(date);
            }

            /**
             * 获取第几月
             * @param date 时间
             * @return
             */
            public static int getMonth(Date date) {
                return toCalendar(date).get(Calendar.MONTH);
            }

            /**
             * 获取第几周
             * @param date 时间
             * @return
             */
            public static int getWeek(Date date) {
                return toCalendar(date).get(Calendar.WEEK_OF_MONTH);
            }

            /**
             * 万年历
             * @param date
             * @return
             */
            public static Calendar toCalendar(Date date) {
                Calendar cal = Calendar.getInstance(Locale.getDefault(Locale.Category.FORMAT));
                cal.setFirstDayOfWeek(Calendar.WEEK_OF_MONTH);
                cal.setTime(date);
                return cal;
            }

            public static void main(String[] args) {

                String monthNoAndWeekNo = getMonthNoAndWeekNo(LocalDateTime.now());
                System.out.println("weekNoOfMonth :" + monthNoAndWeekNo);

                String monthNoAndWeekNo1 = getMonthNoAndWeekNo(LocalDate.now());
                System.out.println("weekNoOfMonth1 :" + monthNoAndWeekNo1);

                String monthNoAndWeekNo2 = getMonthNoAndWeekNo(new Date());
                System.out.println("weekNoOfMonth2 :" + monthNoAndWeekNo2);
                WeekFields weekFields= WeekFields.ISO;
                LocalDate now = LocalDate.now();
                System.out.println(now);
                LocalDate mondayDate = now.with(weekFields.dayOfWeek(), 1L);
                LocalTime time = LocalTime.of(0, 0, 0);
                LocalDateTime monday = LocalDateTime.of(mondayDate, time);
                System.out.println(monday);
                System.out.println("----------------");
                LocalDate today = LocalDate.now();
                System.out.println(today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)));
                System.out.println(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY)));
                System.out.println(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY)));
                System.out.println(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY)));
                System.out.println(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
                System.out.println(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)));
                System.out.println(today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)));

            }



    }
